package com.fitnessapp.main;

import java.util.List;
import java.util.Map;

import com.fitnessapp.database.DatabaseStrings;
import com.fitnessapp.database.MySqlHelper;
import com.fitnessapp.database.RemoteStorageConnection;
import com.fitnessapp.user.User;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class LoginActivity extends Activity {
	
	private ProgressDialog progress;
	private User user;
	
	private Handler handler = new Handler(){
		 @Override
	        public void handleMessage(Message msg) {
			 	//if a user was found then go to main menu
			 	//if not then display bad login info
			 	if( user != null ){
			 		FitnessApplication app = (FitnessApplication)getApplication();
			 	//	app.setUser(user);
			 		
			 		Intent intent = new Intent(LoginActivity.this, MainMenuActivity.class);
			 		startActivity(intent);			 		
			 	}else{
			 		Log.i(getLocalClassName(), "Wrong username or password");
			 	}
	        	
	        }
	};
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.activity_login);
		
	//	Button loginBtn = (Button)findViewById(R.id.login);
		
//		loginBtn.setOnClickListener(new OnClickListener() {
//			
//			public void onClick(View v) {
//				//check the user name and password
//				String username = ((TextView)findViewById(R.id.usernameInput)).getText().toString();
//				String password = ((TextView)findViewById(R.id.passwordInput)).getText().toString();
//				
//				
//				progress = ProgressDialog.show(LoginActivity.this, "", "Authenticating...", true);	
//				
//				(new LoginAuthenticator(username, password)).start();		
//			}
//		});
		
	}
	
	private class LoginAuthenticator extends Thread {
		private String username;
		private String password;
		public LoginAuthenticator(String username, String password){
			this.username = username;
			this.password = password;
		}

		public void run(){
						
			if( authenticate() ){
				loadUser();
			}
			
			progress.dismiss();
			
			handler.handleMessage(null);
		}
		
		//TODO: Move this to web server
		private boolean authenticate(){
			
			MySqlHelper dbHelper = new MySqlHelper(DatabaseStrings.DB_URL);
			List<Map<String,String>> result = dbHelper.query(
					"user_id, user_password", DatabaseStrings.USER_TABLE, "user_name = \'" + username + "\'");			
		
			if( result.size() > 0 ){
				Map<String,String> userData = result.get(0);
				
				Log.d(getLocalClassName(), "Authenticating password: " + userData.get("user_password") + " == " + password);
				
				if(userData.get("user_password").equals(password)){
					//user = new User(userData.get("user_id"), username);
					return true;
				}	
			
			}
			
			return false;			
		}
		
		private void loadUser(){
			if( user != null){
				MySqlHelper dbHelper = new MySqlHelper(DatabaseStrings.DB_URL);
				//load routines
				//user.loadRoutines(dbHelper);
				
			}
		}
	}

}
